﻿/// <reference path="../../scripts/typings/jquery/jquery.d.ts" />
/// <reference path="../../scripts/typings/knockout/knockout.d.ts" />
/// <reference path="../../scripts/typings/durandal/durandal.d.ts" />
import Server = require("../../Scripts/photoweb/Server");
import Album = require("../../Scripts/photoweb/Models/Album");
import router = require("plugins/router");

class AlbumsViewModel {
    public isLoading: KnockoutObservable<boolean>;

    constructor(public albums: KnockoutObservableArray<Album>) {
        this.albums = ko.observableArray<Album>();
        this.isLoading = ko.observable<boolean>(true);
    }

    public activate = (): void => {
        this.isLoading(true);
        Server.getAlbums(
            (response: Album[]): void=> {
                this.isLoading(false);
                this.albums(response);
            },
            (err: JQueryXHR): void=> {
                this.isLoading(false);
                alert(JSON.stringify(err));
            }
            );
    };

    loadPicturesForAlbum = (album: Album) => {
        router.navigate("#photos/" + encodeURIComponent(album.id));
    }
}

export = AlbumsViewModel;